import { IncomingHttpHeaders } from 'http'
import { IncomingRequestCfProperties } from './interfaces/incoming-request-cf.interface'

export function extractCfAnalytics(
  headers: IncomingHttpHeaders, secret?: string,
): IncomingRequestCfProperties | undefined {
  const cf = headers[`x-boticord-analytics${secret ? '-' + secret.toLowerCase() : ''}`] as string | undefined
  if (!cf) return

  try {
    return JSON.parse(cf)
  } catch (e) {
    return
  }
}